package main

import (
	"bufio"
	"errors"
	"fmt"
	"nat_detect"
	"os"
	"strings"
)

func getP2pUid() (string, error) {
	//todo fix me
	p2pUidfile := "/usr/local/bin/p2puid"

	f, err := os.Open(p2pUidfile)
	if err != nil {
		return "", err
	}
	p2pUid := ""
	buf := bufio.NewReader(f)
	for {
		p2pUid, _ = buf.ReadString('\n')
		p2pUid = strings.TrimSpace(p2pUid)
		p2pUid = strings.Trim(p2pUid, "\r")
		break
	}

	if len(p2pUid) == 0 { //没有值时，继续进行，因为有可能有公网IP或upnp
		err = errors.New("p2puid empty")
		fmt.Println(err)
	}
	return p2pUid, err
}

func main() {
	nattype, multiIP := nat_detect.DoTest()
	fmt.Println("[nat type]: ", nattype)
	fmt.Println("[multi ip]: ", multiIP)
	/*
		url := "http://appservice.nextstorage.cn:8080/storageNode/updateDeviceNat"
		p2pid , err := getP2pUid()
		if err != nil{
			fmt.Println("getP2pUid err")
			return
		}

		//
		var upload =  struct {
			NatType string `json:"natType"`
			DeviceNo string `json:"deviceNo"`
			MultiIP int `json:"multiIP"`
		}{
			NatType:  strconv.Itoa(nattype),
			DeviceNo: p2pid,
			MultiIP: multiIP,
		}
		//fmt.Println("multiIP",multiIP)
		data , _  := json.Marshal(upload)
		//fmt.Println("data!!! ",string(data))

		respone , err := http.Post(url, "application/json",bytes.NewBuffer(data))
		if err != nil{
			fmt.Println("Post error", err)

			return
		}
		defer respone.Body.Close()
		data2, _ := ioutil.ReadAll( respone.Body)
		fmt.Println(p2pid,respone.StatusCode, string(data2))

	*/
}
